package org.grow.auth.service;

import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.DisabledException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Service;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author: xwg
 * @CreateDate: 2021/10/14
 */

@Service
public class DefaultAuthFailureHandler
        implements AuthenticationFailureHandler {
    @Override
    public void onAuthenticationFailure(
            HttpServletRequest httpServletRequest,
            HttpServletResponse httpServletResponse,
            AuthenticationException e) throws IOException, ServletException {

        if (e instanceof BadCredentialsException) {
            httpServletResponse.setStatus(710);
            httpServletResponse.getWriter().println(e.getMessage());
        }else if(e instanceof DisabledException){
            httpServletResponse.setStatus(711);
            httpServletResponse.getWriter().println(e.getMessage());
        }else {
            httpServletResponse.setStatus(719);
            httpServletResponse.getWriter().println("未知登录异常："+e.getMessage());
        }



    }
}
